package editorsvg.gui.generico;

import editorsvg.gui.generico.ModeloLista;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * Panel generico de recepción de valores listados en las leyendas
 * @author Jorge Berjano
 */
public class PanelEdicionLista extends javax.swing.JPanel {

    private ModeloLista modeloListaValores;
    private List listaValores = new ArrayList();

    /** Creates new form PanelEdicionLista */
    public PanelEdicionLista() {
        initComponents();

        asignarListaValores(null);
        agregarValor();
    }

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jListaValores = new javax.swing.JList();
        botonNuevo = new javax.swing.JButton();
        cajaTexto = new javax.swing.JTextField();

        jListaValores.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListaValores.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jListaValoresKeyPressed(evt);
            }
        });
        jListaValores.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListaValoresValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(jListaValores);

        botonNuevo.setText("Nuevo");
        botonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoActionPerformed(evt);
            }
        });

        cajaTexto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cajaTextoKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(cajaTexto, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(botonNuevo)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(botonNuevo)
                    .add(cajaTexto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jListaValoresValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListaValoresValueChanged
        if (!evt.getValueIsAdjusting()) {
            editarValorSeleccionado();
        }
    }//GEN-LAST:event_jListaValoresValueChanged

    private void cajaTextoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cajaTextoKeyReleased
        modificarValor();
    }//GEN-LAST:event_cajaTextoKeyReleased

    private void jListaValoresKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jListaValoresKeyPressed
        procesarTecla(evt);
    }//GEN-LAST:event_jListaValoresKeyPressed

    private void botonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoActionPerformed
        agregarValor();
    }//GEN-LAST:event_botonNuevoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonNuevo;
    private javax.swing.JTextField cajaTexto;
    private javax.swing.JList jListaValores;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    private void procesarTecla(KeyEvent evt) {
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            eliminarValor();
        } else if (evt.getKeyCode() == KeyEvent.VK_INSERT) {
            agregarValor();
        } else if (evt.getKeyCode() != KeyEvent.VK_UP && evt.getKeyCode() != KeyEvent.VK_DOWN) {
            cajaTexto.requestFocus();
        }
    }

    private void agregarValor() {
        cajaTexto.setText("");
        listaValores.add("");
        actualizarLista();
        jListaValores.setSelectedIndex(listaValores.size() - 1);
        cajaTexto.requestFocus();

    }

    private void modificarValor() {
        int indice = jListaValores.getSelectedIndex();
        if (indice != -1) {
            listaValores.set(indice, cajaTexto.getText());
            actualizarLista();

        }
    }

    private void eliminarValor() {
        listaValores.remove((jListaValores.getSelectedIndex()));
        actualizarLista();
    }

    private void actualizarLista() {
        if (modeloListaValores != null) {
            modeloListaValores.actualizar();
        }
    }

    private void editarValorSeleccionado() {
        int indice = jListaValores.getSelectedIndex();
        if (indice != -1) {
            cajaTexto.setText(listaValores.get(indice).toString());
        }
    }

    public void asignarListaValores(List lista) {
        listaValores.clear();

        if (lista != null) {
            for (Object elemento : lista) {
                listaValores.add(elemento);
            }
        }

        modeloListaValores = new ModeloLista(listaValores);
        jListaValores.setModel(modeloListaValores);
    }

    public List obtenerListaValores() {
        return listaValores;
    }
//
//    public String obtenerTextoValores() {
//        StringBuilder textoValores = new StringBuilder();
//        for (Object elemento : listaValores) {
//            textoValores.append(elemento);
//            textoValores.append("\t");
//        }
//        return textoValores.toString();
//    }
}
